package k.daniel.databindingdemo;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import k.daniel.databindingdemo.databinding.ActivityMainBinding;
import k.daniel.databindingdemo.databinding.LayoutMainRecyclerItemBinding;
import k.daniel.databindingdemo.model.ActivityModel;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                throwable.printStackTrace();
            }
        });
        ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        binding.setActivity(this);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        ActivityInfo[] activities ;
        try {
            activities = getActivityList();
            binding.recyclerView.setAdapter(new MyAdapter(ActivityModel.parse(activities)));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有当前包名下的activity信息
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    private ActivityInfo[] getActivityList() throws PackageManager.NameNotFoundException {
        PackageManager pm = this.getPackageManager();
        PackageInfo info = pm.getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
        ActivityInfo[] list = info.activities;
        return list;
    }

    public void jumpToVariableActivity(View v)
    {
        startActivity(new Intent(getApplicationContext(),TwoWayDataBindingActivity.class));
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHodler>{
        List<ActivityModel> activityInfos;

        public MyAdapter(List<ActivityModel> activityInfos) {
            this.activityInfos = activityInfos;
        }

        @Override
        public MyViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
             LayoutMainRecyclerItemBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.layout_main_recycler_item, parent, false);
            binding.getRoot().setTag(binding);
            MyViewHodler holder = new MyViewHodler(binding.getRoot());
            holder.setBinding(binding);
            return  holder;
        }

        @Override
        public void onBindViewHolder(MyViewHodler holder, int position) {
            ActivityModel activityInfo=activityInfos.get(position);
//            LayoutMainRecyclerItemBinding binding = (LayoutMainRecyclerItemBinding) holder.itemView.getTag();
//            binding.setItem(activityInfo);
            holder.getBinding().setVariable(k.daniel.databindingdemo.BR.item,activityInfos.get(position));
            holder.getBinding().executePendingBindings();
        }

        @Override
        public int getItemCount() {
            return activityInfos.size();
        }

        class MyViewHodler extends RecyclerView.ViewHolder{

            private ViewDataBinding binding;

            public MyViewHodler(View itemView) {
                super(itemView);
            }

            public void setBinding(ViewDataBinding binding) {
                this.binding = binding;
            }

            public ViewDataBinding getBinding() {
                return this.binding;
            }
        }
    }
}
